//
//  WXPSubmitOrderSuccessVC.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/6.
//

#import "WXPSubmitOrderSuccessVC.h"

@interface WXPSubmitOrderSuccessVC ()

@property (nonatomic, strong) WXPNavView  * navView;
@property (nonatomic, strong) UIView      * headerView;
@property (nonatomic, strong) UIImageView * imageTop;
@property (nonatomic, strong) UILabel     * labelTitle;
@property (nonatomic, strong) UILabel     * labelContent;
@property (nonatomic, strong) UIButton    * btnOK;
@property (nonatomic, strong) WXPCommonAgreementView * agreementView;

@end

@implementation WXPSubmitOrderSuccessVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (!_navView) {
        _navView = [WXPNavView new];
        [_navView resetViewWithTitle:@"Solicitud de pedido"];
        _navView.blockPop = ^{
            [Global_Nav popToRootViewControllerAnimated:true];
        };
    }
    return _navView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [UIView new];
        _headerView.widthHeight = XY(SCREEN_WIDTH, 0);
        _headerView.userInteractionEnabled = true;
        _headerView.backgroundColor = [UIColor clearColor];
    }
    return _headerView;
}
- (UIImageView *)imageTop{
    if (!_imageTop) {
        _imageTop = [UIImageView new];
        _imageTop.widthHeight = XY(85, 84);
        _imageTop.image = [UIImage imageNamed:@"icon-submit-order-success"];
    }
    return _imageTop;
}
- (UILabel *)labelTitle{
    if (!_labelTitle) {
        _labelTitle = [UILabel new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.font = [UIFont boldSystemFontOfSize:14];
        [_labelTitle fitTitle:@"La solicitud de préstamo ha sido enviada." variable:SCREEN_WIDTH-30-30];
    }
    return _labelTitle;
}
- (UILabel *)labelContent{
    if (!_labelContent) {
        _labelContent = [UILabel new];
        _labelContent.textColor = COLOR_CONTENT;
        _labelContent.font = [UIFont systemFontOfSize:12];
        _labelContent.numberOfLines = 0;
        _labelContent.lineSpace = 3;
        [_labelContent fitTitle:@"La revisión crediticia se actualizará en 24 horas. Inicie sesión en la app para verificar los resultados." variable:SCREEN_WIDTH-30-30];
    }
    return _labelContent;
}
- (UIButton *)btnOK{
    if (!_btnOK) {
        _btnOK = [UIButton new];
        _btnOK.backgroundColor = COLOR_PRIMARY;
        _btnOK.widthHeight = XY(SCREEN_WIDTH-30-30, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnOK color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        _btnOK.titleLabel.fontNum = 14;
        [_btnOK setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [_btnOK setTitle:@"OK" forState:UIControlStateNormal];
        WEAKSELF
        [_btnOK setNoRepeatActionWithInterval:1 action:^(UIButton * _Nonnull button) {
            [NSObject clickFeedback];
            [weakSelf.btnOK addClickAnimate];
            [GlobalMethod showAlert:@"Gracias por su apoyo"];
            [[NSNotificationCenter defaultCenter] postNotificationName:TABBAR_CHANGE_TO_ORDER object:nil userInfo:nil];
            [Global_Nav popToRootViewControllerAnimated:true];
        }];
    }
    return _btnOK;
}
- (WXPScoreView *)scoreView
{
    if (_scoreView == nil) {
        _scoreView = [WXPScoreView  new];
        WEAKSELF
        _scoreView.blockSubmitScore = ^{
            [weakSelf requestSubmitScore];
        };
    }
    return  _scoreView;
}
- (WXPCommonAgreementView *)agreementView{
    if (!_agreementView) {
        _agreementView = [WXPCommonAgreementView new];
        _agreementView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-LEFT_MARDIN);
    }
    return _agreementView;
}

#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableView.backgroundColor = COLOR_BACKGROUND;
    
    [self configUI];
    if(self.isShowScoreView == true){
        UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
        [window addSubview:self.scoreView];
    }
}

- (void)configUI{
    self.navView.leftTop = XY(0, 0);
    [self.view addSubview:self.navView];
    
    self.imageTop.centerXTop = XY(SCREEN_WIDTH/2, Ratio(86));
    [self.headerView addSubview:self.imageTop];
    
    self.labelTitle.centerXTop = XY(SCREEN_WIDTH/2, self.imageTop.bottom+Ratio(32));
    [self.headerView addSubview:self.labelTitle];
    
    self.labelContent.centerXTop = XY(SCREEN_WIDTH/2, self.labelTitle.bottom+Ratio(16));
    [self.headerView addSubview:self.labelContent];
    
    self.btnOK.centerXTop = XY(SCREEN_WIDTH/2, self.labelContent.bottom+Ratio(86));
    [self.headerView addSubview:self.btnOK];
    self.headerView.height = self.btnOK.bottom+60;
    self.tableView.tableHeaderView = self.headerView;
    
    [self.view addSubview:self.agreementView];
    self.tableView.height = self.agreementView.top-10-self.navView.bottom;
    self.tableView.top = self.navView.bottom;
}

#pragma mark - request
- (void)requestSubmitScore{
    [RequestApi requestSubmitRatingWithScore:self.scoreView.score suggest:self.scoreView.contentTextView.text isFromFalseProduct:0 delegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        if(self.scoreView.score == 4){
            // App Store评分窗口，让用户打分
            //NSString *appStoreURL = [NSString stringWithFormat:@"https://apps.apple.com/app/%@",APPLEID];
            NSString *appStoreURL = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/%@?action=write-review",APPLEID];
            
            NSURL *url = [NSURL URLWithString:appStoreURL];
            if ([[UIApplication sharedApplication] canOpenURL:url]) {
                [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
            }
        }
        [self.scoreView removeFromSuperview];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

@end
